"""
活动表添加场馆预订时间字段
日期: 2024-01-20
"""

from datetime import datetime
import mysql.connector
from mysql.connector import Error

def upgrade_database():
    """升级数据库：添加场馆预订时间字段"""
    try:
        # 连接数据库
        connection = mysql.connector.connect(
            host='1.92.133.195',
            database='weixin',
            user='weixin',
            password='k5Ykx2WSmjHJaPFs',
            port=3306
        )
        
        if connection.is_connected():
            cursor = connection.cursor()
            
            # 添加场馆预订时间字段
            alter_queries = [
                "ALTER TABLE activities ADD COLUMN venue_start_time DATETIME NULL COMMENT '场馆预订开始时间' AFTER registration_deadline",
                "ALTER TABLE activities ADD COLUMN venue_end_time DATETIME NULL COMMENT '场馆预订结束时间' AFTER venue_start_time"
            ]
            
            for query in alter_queries:
                try:
                    cursor.execute(query)
                    print(f"执行成功: {query}")
                except Error as e:
                    if "Duplicate column name" in str(e):
                        print(f"字段已存在，跳过: {query}")
                    else:
                        print(f"执行失败: {query}, 错误: {e}")
            
            connection.commit()
            print("数据库升级完成")
            
    except Error as e:
        print(f"数据库连接失败: {e}")
    finally:
        if connection.is_connected():
            cursor.close()
            connection.close()

if __name__ == "__main__":
    upgrade_database() 